package com.ellecity06.common.base

import android.app.Activity
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.util.DisplayMetrics
import android.view.WindowManager
import android.widget.PopupWindow
import androidx.viewbinding.ViewBinding

/**
 * @author ellecity06
 * @time 2024/12/3 14:22
 * @des popupwindow 的基础类
 */
abstract class BasePopupWindow<VB : ViewBinding>(private val ctx: Activity) : PopupWindow(ctx) {
    val binding: VB by lazy { inflaterViewBinding(ctx) }
    private val mDisplayMetrics: DisplayMetrics by lazy { ctx.resources.displayMetrics }
    private val screenWidth: Int by lazy { mDisplayMetrics.widthPixels }
    private val screenHeight: Int by lazy { mDisplayMetrics.heightPixels }


    init {
        onCreate(ctx)

    }

    private fun onCreate(ctx: Context) {
        contentView = binding.root
        setBackgroundDrawable(ColorDrawable())
        isFocusable = true
        isOutsideTouchable = true
        isTouchable = true

        initialize(ctx)
    }

    fun getWidthP(widthP: Float): Int {

        return (screenWidth * widthP).toInt()
    }

    fun getHeightP(heightP: Float): Int {
        return (screenHeight * heightP).toInt()
    }

    fun setBackgroundAlpha(alpha: Float) {
        val activity: Activity = ctx
        val layoutParams: WindowManager.LayoutParams = activity.window.attributes
        layoutParams.alpha = alpha
        // 解决华为手机背景变暗无效问题
        activity.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        activity.window.attributes = layoutParams
    }

    abstract fun inflaterViewBinding(ctx: Context): VB
    abstract fun initialize(ctx: Context)
}